package com.example.demo.utils;

import com.example.demo.dataobject.HealthInfo;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtil {

    /**
     * 封装json
     * @param type
     * @return
     */
    public static String toJson(int type){
        JSONObject jsonObject = new JSONObject();
        String jsonString="";
        try {
            jsonObject.put("type",type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonString=jsonObject.toString();
        return jsonString;
    }

    public static String dataToJson(HealthInfo healthInfo){
        JSONObject jsonObject = new JSONObject();
        String jsonString="";
        try{
            jsonObject.put("identity",healthInfo.getIdentity());
//            jsonObject.put("nickname",healthInfo.getNickname());
            jsonObject.put("sex",healthInfo.getSex());
            jsonObject.put("birthYear",healthInfo.getBirthYear());
            jsonObject.put("birthMonth",healthInfo.getBirthMonth());
            jsonObject.put("weight",healthInfo.getWeight());
            jsonObject.put("height",healthInfo.getHeight());
            jsonObject.put("laborIntensity",healthInfo.getLaborIntensity());
            jsonObject.put("diabetesType",healthInfo.getDiabetesType());
            jsonObject.put("diagnosisYear",healthInfo.getDiagnosisYear());
            jsonObject.put("diagnosisMonth",healthInfo.getDiagnosisMonth());
            jsonObject.put("treatment",healthInfo.getTreatment());
            jsonObject.put("complication",healthInfo.getComplication());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonString=jsonObject.toString();
        System.out.println(jsonString);
        return jsonString;
    }

}
